package com.example.keli.ui.home.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.keli.MainActivity;
import com.example.keli.R;
import com.example.keli.api.HttpUtils;
import com.example.keli.api.RetrofitManager;
import com.example.keli.api.http.api.BaseResponse;
import com.example.keli.api.http.api.subscriber.SubscriberListener;
import com.example.keli.base.BaseActivity;
import com.example.keli.ui.home.adapter.UserListAdapter;
import com.example.keli.ui.home.model.DetailUserData;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * description: 贡献榜单
 * author: Allen
 * date: 2019/3/22 14:47
 */
public class ContributeActivity extends BaseActivity {

    String uid = "";
    String aaid = "";
    String nickName = "";

    UserListAdapter userAdapter;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.refreshlayout)
    SwipeRefreshLayout refreshlayout;
    List<DetailUserData.UserInfoBean> mUserList = new ArrayList<>();
    @BindView(R.id.jump_home)
    ImageView jumpHome;

    public static void start(Context context, String uid, String aaid, String nick_name) {
        Intent intent = new Intent(context, ContributeActivity.class);
        intent.putExtra("uid", uid);
        intent.putExtra("aaid", aaid);
        intent.putExtra("nick_name", nick_name);
        context.startActivity(intent);
    }

    @Override
    protected int layoutId() {
        return R.layout.activity_contribute;
    }

    @Override
    protected void bindData() {
        uid = getIntent().getStringExtra("uid");
        aaid = getIntent().getStringExtra("aaid");
        nickName = getIntent().getStringExtra("nick_name");

        abTitle.setText(nickName + "的参赛信息");
        recyclerview.setLayoutManager(new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL));
        userAdapter = new UserListAdapter(mUserList);
        recyclerview.setAdapter(userAdapter);
        getActorLikeList();

        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void bindListener() {
        refreshlayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if (refreshlayout.isRefreshing()) {
                    refreshlayout.setRefreshing(false);
                }
            }
        });
        userAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {

            }
        },recyclerview);
    }

    //贡献榜单
    private void getActorLikeList() {
        Map<String, String> map = new HashMap<>();
        map.put("uid", uid);
        map.put("aaid", aaid);
        HttpUtils.ApiFunc(RetrofitManager.mApiFunc.getActorLikeList(map), new SubscriberListener<BaseResponse<DetailUserData>>() {
            @Override
            public void onSuccess(BaseResponse<DetailUserData> response) {
                mUserList.clear();
                mUserList.addAll(response.getData().getUserInfo());
                userAdapter.notifyDataSetChanged();
                userAdapter.loadMoreEnd();

                if (refreshlayout.isRefreshing()) {
                    refreshlayout.setRefreshing(false);
                }
            }

            @Override
            public void onFailer(String msg) {
                if (refreshlayout.isRefreshing()) {
                    refreshlayout.setRefreshing(false);
                }
            }

            @Override
            public void onTokenError() {
            }
        });
    }

    @OnClick(R.id.jump_home)
    public void onViewClicked() {
        MainActivity.start(ContributeActivity.this);
    }
}
